﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MemberPoint.Business;
using MemberPoint.Common;
using MemberPoint.Entity.POCOModels;
using MemberPoint.Entity.ViewModels.LoginViewModel;

namespace MemberPoint.Business
{
    public class LoginService: BaseService<Users>
    {
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        #region
        public OperateResult Login(LoginViewModel viewModel)
        {
            var model = Select(u => u.U_LoginName == viewModel.U_LoginName && u.U_Password == viewModel.U_Password);
            if (model != null) 
            {
                return new OperateResult(ResultStatus.Success, "登录成功！");
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "用户名或密码错误！");
            }
        }
        #endregion

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        #region
        public OperateResult UpdatePwd(UpdatePasswordViewModel viewModel)
        {
            var model = Select(u => u.U_ID == viewModel.U_ID && u.U_Password == viewModel.U_OldPwd);
            if (model != null) 
            {
                if (viewModel.U_NewPassword == viewModel.U_PasswordTwice) 
                {
                    model.U_Password = viewModel.U_NewPassword;
                    if (Update(model)) 
                    {
                        return new OperateResult(ResultStatus.Success, "修改密码成功，需重新登录！");
                    }
                    else
                    {
                        return new OperateResult(ResultStatus.Error, "服务器繁忙，请稍后重试！");
                    }
                }
                else
                {
                    return new OperateResult(ResultStatus.Error, "两次密码输入不一样！");
                }
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "旧密码输入错误！");
            }
        }
        #endregion

        /// <summary>
        /// 修改信息前查询
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        #region
        public OperateResult Info(UpdateInfoViewModel viewModel)
        {
            var model = Select(u => u.U_ID == viewModel.U_ID);
            if (model != null) 
            {
                viewModel.U_LoginName = model.U_LoginName;
                viewModel.U_RealName = model.U_RealName;
                viewModel.U_Sex = model.U_Sex;
                viewModel.U_Telphone = model.U_Telephone;
                return new OperateResult(ResultStatus.Success, "", model);
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "没有该用户信息！");
            }
        }
        #endregion

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        #region
        public OperateResult UpdateInfo(UpdateInfoViewModel viewModel)
        {
            var model = Select(u => u.U_ID == viewModel.U_ID);
            if (model != null) 
            {
                model.U_LoginName = viewModel.U_LoginName;
                model.U_RealName = viewModel.U_RealName;
                model.U_Sex = viewModel.U_Sex;
                model.U_Telephone = viewModel.U_Telphone;
                if (Update(model)) 
                {
                    return new OperateResult(ResultStatus.Success, "信息修改成功！");
                }
                else
                {
                    return new OperateResult(ResultStatus.Error, "系统繁忙，请稍后重试！");
                }
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "没有该用户信息！");
            }
        }
        #endregion
    }
}
